%% Solve for the steady state
%  and in response to MIT shock to TFP

% asymmetric labor adjustment cost
% fixed mass of potential entrants, each of which gets a signal about 
% their future productivity

% entrants have lower average productivity than do incumbents
% and they slowly grow

%% Set Options
% All
options.Nbell       = 100;        % Number of Bellman iterations before Newton.
options.Nnewt       = 15;       % Maximum number of Newton steps
options.tolc        = 1e-12;     % Tol on value functions
options.T_irf       = 50;       % Number of periods for IRFs
options.T           = 300;     % Number of periods for MIT shock
options.Terg        = 500;      % Number of periods to burn
options.hpisteps    = 100;

% Stationary
options.L = [];
options.itermaxL    = 5000;     % Max number of iterations to find L
options.tolL        = 1e-12;    % Tol for L
options.tolK        = 1e-5;     % Tol for equilibrium K
options.itermaxK    = 100;      % Max iterations for bisection
options.cresult = [];

options.tolD        = 1e-08;
options.itermaxp    = 25;

options.damp = 0;
options.damp_mit = .99;
options.damp_DC  = .9;

optset('bisect', 'tol', 1e-12)

%% Set globals and parameters
% % try vavra numbers
glob.n          = [30,30];   % Number of nodes for b,a
glob.nf         = [100,30];

glob.spliorder  = [1,1];    % Order of splines (Envelope Condition Method seems only robust when quadratic in k)
glob.Na         = 30;           % Number of nodes for quadrature
glob.curv       = .1;            % Amount of curvature for p grid


glob.minb = 0;
glob.maxb = 1;


%% Model parameters

% preferences
param.beta      = 0.96;               

% idiosyncratic shock
param.rho_s           = 0.86;
param.sigma_s         = 0.22;

% labor adjustment cost
param.phi_L           = 0.042596;

% Demand parameters
param.sigma           = 3.9395;              % average elasticity
param.epsilon         = 2.6042*param.sigma;     % epsilon/sigma is superelasticity, 0 is CES 

param.omega           = 1;                  
         
param.A = 1;

% entry and exit
param.gamma = .08;     % exogenous exit rate

% fixed cost of production
param.mu_f     = -1e10;
param.sigma_f  = .9;

% dist of log fixed cost of entry 
param.ce_mu    = -5.3625;      
param.ce_sigma = .5; % was .5

param.M        = 1e2;

% dist of entrant productivity is diff from incumbent
param.d_E     = 0.73342;

param.nu      = 1/2; % inverse Frisch elasticity
                     % same as in clementi
                     % can calibrate aggregate shock size to hit moments

% free labor adjustment 
param.delta    = 0;

glob.mink       = 0;
glob.maxk       = 1e8;

%% Find initial steady state
options.itermax_DC = 100000;

[param,glob]    = setup(param,glob,options);

eq_SS           = solve_eq_ss(param, glob, options);
save('eq_SS.mat', 'eq_SS', 'param', 'glob', 'options');
% param.mu_entry  = eq_SS.mu_entry;
% param.mu_f      = param.mu_entry - param.mu_f_diff*abs(param.mu_entry);

glob.agg = kimball_agg(1, eq_SS, param, glob); % Fix this value!
L        = sum(eq_SS.l.*eq_SS.L);
param.psi = 1/(L^param.nu); % psi = 1/(L^nu*C)

% check that C = 1... 
tosolve = @(C) kimball_agg(C, eq_SS, param, glob) - glob.agg;

fprintf('C is %.5f \n', bisect(tosolve, .5, 1.5));

mom = compute_moments(eq_SS, param, glob, options);
%% Try MIT shock - fall in TFP

% sim = set_up_mit(eq_SS, param, glob, options);
% 
% solve_eq_mit(sim, eq_SS, param, glob, options)

 
%% do one solve_bf
%sim_out  = solve_bf(sim, eq_SS, param, glob, options);


%% Try MIT shock - fall in entry
 sim = set_up_mit_entry(eq_SS, param, glob, options);
% %  
 solve_eq_mit(sim, eq_SS, param, glob, options)
% 
